当前位置: 首页 > 编程日记 > 正文

maven学习(4)-Maven 构建Web 项目

紧接着上一节(3),现在maven新建web项目,user-web。模拟一个用户登录的需求:

工程结构:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cy.user</groupId><artifactId>user-web</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><dependencies><!-- 添加Servlet支持 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version></dependency><!-- 添加jstl支持 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- 添加Spring支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>com.cy.user</groupId><artifactId>user-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><finalName>user-web</finalName></build>
</project>

注意上面user-web项目添加了第3节里面的user-service依赖,需要将user-service maven install发布到本地仓库;

但是不需要添加user-dao的依赖,因为user-service里面已经依赖了user-dao。依赖的继承;

src/main/resources/srping-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:p="http://www.springframework.org/schema/p"  xmlns:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"  xmlns:jee="http://www.springframework.org/schema/jee"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    <!-- 使用注解的包,包括子集 --><context:component-scan base-package="com.cy.user.controller" /><!-- 视图解析器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/" /><property name="suffix" value=".jsp"></property></bean></beans>  

webapp/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>user-web</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- Spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 编码过滤器 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Spring监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 添加对springmvc的支持 --><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>

com.cy.user.controller.UserController.java:

package com.cy.user.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cy.user.entity.User;
import com.cy.user.service.UserService;@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户登录* @param user* @param request* @return*/@RequestMapping("/login")public String login(User user,HttpServletRequest request){User resultUser=userService.login(user);if(resultUser==null){request.setAttribute("user", user);request.setAttribute("errorMsg", "用户名或密码错误!");return "index";}else{HttpSession session=request.getSession();session.setAttribute("currentUser", resultUser);return "redirect:/success.jsp";}}
}
View Code

注意上面的重定向:return "redirect:/success.jsp";

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login.do" method="post">userName:<input type="text" name="userName" value="${user.userName }"/><br/>password:<input type="password" name="password" value="${user.password }"><br/><input type="submit" value="login"/><font color="red">${errorMsg }</font>
</form>
</body>
</html>
View Code

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎:${currentUser.userName }
</body>
</html>
View Code

最后访问http://localhost:8080/user-web/index.jsp进行测试;

--------

转载于:https://www.cnblogs.com/tenWood/p/8638052.html

相关文章:

如何查看linux版本

1. 查看内核版本命令&#xff1a; 1) [rootq1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompilecrowe.devel.redhat.com) (gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)) #1 SMP Mon Sep 19 18:00:54 EDT 2005 2) [rootq1test01 ~]# uname -a …

存储过程由结构表生成表

结构表 CREATE TABLE JGTB5001( ZDM VARCHAR2(30 BYTE), HZM VARCHAR2(100 BYTE), LX VARCHAR2(50 BYTE), JD VARCHAR2(20 BYTE), WBKLX VARCHAR2(100 BYTE), FUNCTIONNAME VARCHAR2(50 BYTE), FUNCTIONPARAMETER VARCHAR2(50 BYTE)); 生成的TB表CREATE OR REPLACE PROCEDURE P…

好礼相送|CSDN云原生 Meetup 成都站报名热烈启动,12.18见!

伴随着容器、Kubernetes及微服务等技术热度的持续攀升&#xff0c;云原生正以不可撼动之势&#xff0c;剑指云计算的下一个十年。12月18日&#xff0c;CSDN将在成都举办第三场云原生线下Meetup。在这里&#xff0c;您可以了解各大领先企业的云原生落地实践&#xff0c;与众多云…

vue-music 音乐网站

在学习完vueJS,一直想做个项目来锻炼一下,选来选去&#xff0c;还是做个网易云音乐&#xff0c;其间遇到了很多坑,也逐渐接受了vue这种组件化的思想以及从Dom操作转换为用数据去驱动视图。并且在某部分基础组件上借鉴(搬运)了elementUI的源码(不过elementUI写的是真好) 技术栈 …

shell环境变量

shell环境变量 环境变量 还记得上一章里面﹐我曾经提到过﹕当我们登入系统的时候﹐首先就获得一 shell﹐而且它也占据一个行程&#xff08;进程&#xff09;﹐然后再输入的命令都属于这个 shell 的子程序&#xff08;子进程&#xff09;。如果您学习够细心﹐不难发现我们的 sh…

apache用户认证

先创建一个“用户认证”目录&#xff08;设为abc&#xff09;[rootLAMPLINUX ~]# cd /data/www[rootLAMPLINUX www]# mkdir abc进入abc目录[rootLAMPLINUX www]# cd abc拷贝一个文件&#xff08;作用&#xff1a;验证配置是否生效&#xff09;[rootLAMPLINUX abc]# cp /etc/pas…

20个经典函数细说 Pandas 中的数据读取与存储,强烈建议收藏

作者 | 俊欣来源 | 关于数据分析与可视化大家好&#xff0c;今天小编来为大家介绍几个Pandas读取数据以及保存数据的方法&#xff0c;毕竟我们很多时候需要读取各种形式的数据&#xff0c;以及将我们需要将所做的统计分析保存成特定的格式。我们大致会说到的方法有&#xff1a;…

fastlane自动打包--详细介绍

fastlane--Packaging 自动化打包&#xff0c;通过fastlane自动发布Fastlane安装不在这里详细罗列&#xff0c;参照一下链接流程 https://www.jianshu.com/p/0a113f754c09操作步骤 1.检查Fastlane是否正确安装。输入以下命令&#xff1a; fastlane --version 复制代码可以看到Fa…

【Big Data】HADOOP集群的配置(一)

Hadoop集群的配置&#xff08;一&#xff09; 摘要: hadoop集群配置系列文档&#xff0c;是笔者在实验室真机环境实验后整理而得。以便随后工作所需&#xff0c;做以知识整理&#xff0c;另则与博客园朋友分享实验成果&#xff0c;因为笔者在学习初期&#xff0c;也遇到不少问题…

C语言 条件编译详解

预处理过程扫描源代码&#xff0c;对其进行初步的转换&#xff0c;产生新的源代码提供给编译器。可见预处理过程先于编译器对源代码进行处理。在C 语言中&#xff0c;并没有任何内在的机制来完成如下一些功能&#xff1a;在编译时包含其他源文件、定义宏、根据条件决定编译时是…

凝聚406万开发者 飞桨十大发布提速产业智能化

12月12日&#xff0c;由深度学习技术及应用国家工程实验室主办的WAVE SUMMIT2021深度学习开发者峰会在上海召开。百度首席技术官、深度学习技术及应用国家工程实验室主任王海峰公布飞桨最新成绩单&#xff1a;凝聚406万开发者、创建47.6万模型、服务15.7万企事业单位&#xff0…

环境变量,cp,mv,查看文档命令

2019独角兽企业重金招聘Python工程师标准>>> 一、环境变量PATH echo $PATH 打印当前的环境变量 PATH$PATH:路径 自定义环境变量 which查找某个命令的绝对路径&#xff0c;也可以查看某个命令的别名&#xff0c;which查找的范围就在PATH下的几个目录下查找&#xff1…

Linux中errno使用

当linux中的C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因&#xff0c;在实际编程中用这一招解决了不少原本看来莫名其妙的问题。但是errno是一个数字&#xff0c;代表的具体含义还要到errno.…

工程师文化:BAT 为什么不喊老板

BAT员工之间不喊老板&#xff0c;也不喊真名&#xff0c;而是用同学、花名&#xff0c;这是虚情假意&#xff1f;还是弘扬武侠文化&#xff1f;还是另有隐情&#xff1f;为什么欧美公司不这么做&#xff1f;本文将带大家走进科学&#xff0c;探索真相。 BAT 的称呼方式 腾讯&am…

SVN常见问题

2019独角兽企业重金招聘Python工程师标准>>> 目录[隐藏] 1. 提示SVN证书过期&#xff1f; 2. 用户名密码校验失败&#xff1f; 3. SVN提交文件时提示文件冲突怎么办&#xff1f; 4. SVN提交文件时提示失败&#xff1f; 1. 提示SVN证书过期&#xff1f; 问题描述&…

2017海克斯康拉斯维加斯美国大会 精彩即将开始

海克斯康集团与遍及全球行业用户的故事已经证明&#xff0c;海克斯康先进的解决方案影响着世界各行各业的发展&#xff0c;并为他们带来了颠覆性的科技变革...... 通过海克斯康集团与遍及全球行业用户的故事&#xff0c;已经证明海克斯康先进的解决方案影响着世界各行各业的发展…

Linux环境编程--waitpid与fork与execlp

waitpidwaitpid(等待子进程中断或结束)表头文件#include<sys/types.h>#include<sys/wait.h>定义函数 pid_t waitpid(pid_t pid,int * status,int options);函数说明waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用 wait()时子进程已经结…

C# 批处理制作静默安装程序包

使用批处理WinRAR制作静默安装程序包 echo 安装完窗口会自动关闭&#xff01;&#xff01;&#xff01; echo off start /wait Lync.exe /Install /Silent start /wait vcredist_x86/vcredist_x86.exe /q /norestart start /wait DotNetFx40/dotNetFx40_Full_x86_x64.exe /q /…

程序员是复制粘贴的工具人?还是掌握“谜底”的魔术师?

作者 | David Heinemeier Hansson译者 | 弯月出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;编程世界在经历了“Imposter Syndrome(冒充者症候群/负担症候群&#xff09;”和“gatekeeping&#xff08;守门人理论&#xff09;”两方的激战之后&#xff0c;最终以“…

Josephus Problem的详细算法及其Python, Java语言的实现

笔者昨天看电视&#xff0c;偶尔看到一集讲述古罗马人与犹太人的战争——马萨达战争&#xff0c;深为震撼&#xff0c;有兴趣的同学可以移步&#xff1a;http://finance.ifeng.com/a/20170627/15491157_0.shtml . 这不仅让笔者想起以前在学数据结构时碰到的Josephus问题&a…

SlightPHP

SlightPHP是一个轻量级的php框架&#xff0c;支持php5&#xff0c;和php模块方式使用&#xff0c;和apc使用性能更高&#xff01;项目地址&#xff1a;http://code.google.com/p/slightphp/源码地址&#xff1a;http://slightphp.googlecode.com/svn/trunk/你有两种方法使用Sli…

bzoj1178

题目&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id1178 看ppthttp://wenku.baidu.com/link?urldJv6LNme7syiLGM-TzbEEKXwx36JWEnI5HFrIlzfmzUXXg4HG8FDggj5WQS3EKL3k3p-sUYeJ268jCvN4t_kq2YPo3I4GXvaGulQjXrO3d7#include<cstdio> #include<cstdlib&…

编程能力差,学不好Python、AI、Java等技术,90%是输在了这点上!

据了解&#xff0c;超90%的人在学习Python、Java、AI等技术时&#xff0c;都是在网上随便找个入门的教程就开始学起来。然而多数人在看了不少教程后&#xff0c;还是很难独立完成项目&#xff0c;甚至反思自己为什么学了这么久编程能力还是这么差&#xff01;因为你在刚刚开始学…

cglib代理的使用

一、什么是CGLIB? 总的来说&#xff0c;无论是cglib、jdk动态代理又或者是aop面向切面编程&#xff0c;都运用到了一个最重要的设计模式--代理模式&#xff01;万变不离其终&#xff0c;学好代理模式&#xff0c;打遍天下无敌手&#xff01; cglib就是一个字节码生成和转换的库…

使用PHP+Sphinx建立高效的站内搜索引擎

1. 为什么要使用Sphinx假设你现在运营着一个论坛&#xff0c;论坛数据已经超过100W&#xff0c;很多用户都反映论坛搜索的速度非常慢&#xff0c;那么这时你就可以考虑使用Sphinx了&#xff08;当然其他的全文检索程序或方法也行&#xff09;。2. Sphinx是什么Sphinx由俄…

9个必知的 Python 操作文件/文件夹方法

作者 | 欣一来源 | Python爱好者集中营近几年随着Python的热度不断上涨&#xff0c;人们渐渐使用这门编程语言来进行一些自动化操作&#xff0c;以节省重复劳动带来的效率低下&#xff0c;那么必定会涉及到对文件系统的操作&#xff0c;包括文件的增、删、改、查等等&#xff0…

Get/POST方法提交的长度限制

&#xfeff;&#xfeff;1. Get方法长度限制 Http Get方法提交的数据大小长度并没有限制&#xff0c;HTTP协议规范没有对URL长度进行限制。这个限制是特定的浏览器及服务器对它的限制。 如&#xff1a;IE对URL长度的限制是2083字节(2K35)。 下面就是对各种浏览器和服务器的…

Bitmap上下合成图片

合成两张图片&#xff0c;上下叠加的效果&#xff1a; /*** 把两个位图覆盖合成为一个位图&#xff0c;以底层位图的长宽为基准** param backBitmap 在底部的位图* param frontBitmap 盖在上面的位图* return*/public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap fr…

PHP 符号大全

注解符号: // 单行注解 /* */ 多行注解引号的使用’ ’ 单引号,没有任何意义,不经任何处理直接拿过来;" "双引号,php动态处理然后输出,一般用于变量.变量形态: 一种是True 即 真的;另一种是False 即假的常见变量形态: string 字串(数字\汉…

添加Net4CollectionTypeFactory的原因

.NET4.0已经实现了该功能 http://jahav.com/blog/nhibernate-using-net-4-iset/ NHibernate using .NET 4 ISet 0 CommentsNHibernate 4.0 (released in 2014-08-17) has brought us support for .NET 4 ISet<> collections, thus freeing us from the tyranny of the Ie…