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
maven学习(4)-Maven 构建Web 项目相关推荐
- maven(3)------maven构建web项目详细步骤
eclipse集成工具,轻松通过maven构建web项目步骤如下: 一, 右键,new -->project, 进入下一页面 二,选择"Maven Project", 点击下 ...
- maven + spring mvc 创建Java web项目
本篇文章,我们会教你通过maven和spring mvc创建一个Java web项目.关于maven如何创建普通的Java项目,上一篇文章已经作了说明. 开发环境: 1.jdk 1.7 2.Maven ...
- 使用Maven tomcat:run命令启动web项目时修改默认端口的方法
在使用maven时,修改插件tomcat启动端口.一开始我在pom.xml配置文件里面修改,如下面所示: 1 2 3 4 5 6 7 8 9 <plugin> <groupId> ...
- gradle构建web项目_25多个Web资源可帮助您构建项目
gradle构建web项目 This article was created in partnership with Mekanism. Thank you for supporting the pa ...
- idea自动构建web项目_构建Web应用程序以自动执行系统管理员任务
idea自动构建web项目 系统管理员(sysadmin)每年在重复性任务上浪费数千小时. 幸运的是,使用开源工具构建的Web应用程序可以自动消除很大一部分痛苦. 例如,使用Python和JavaSc ...
- 电子商城后台系统(一):使用eclipse构建web项目
资源地址:https://pan.baidu.com/s/1MX4t_kT3iNHmtIsO67YqXQ,提取码:nn1k. 使用eclipse构建web项目,需要有eclipse和Tomcat,两者 ...
- idea构建web项目-超详细教程
1.idea构建web项目 idea构建web项目的超级详细教程,一步一步来,完全没问题! 1.新建一个空项目 2.新建java模块,名为webDemo1 3.选择webDemo1右键,选择Add F ...
- 基于Maven构建Web项目
1.下载Maven,并配置好环境变量 2.打开命令行窗口,输入以下命令构建Maven Web项目 mvn archetype:generate -DgroupId=com.hello -Dartifa ...
- 使用Maven构建Web项目-测试
1.在src/main/java下,新建一个Servlet [java] view plaincopy package com.deppon.text01.action; import java.io ...
最新文章
- c语言程序设计第二版第五章课后答案甘勇,郑州工程技术学院副院长甘勇来校讲学和指导工作...
- Windows下Redis中RedisQFork位置调整
- 编译有哪些阶段,动态链接和静态链接的区别 c++
- Java8采用stream、parallelStream迭代的区别
- 数位dp从会打模板到不会打模板
- 使用lt;jsp:includegt;,不想写死URL,动态生成URL的解决的方法
- 讲座记录——大数据共享和交易的挑战与初探
- 欲取代硬盘?SSD固态存储器前景分析
- 【伙伴故事】一盏智能灯,点亮家庭和工业照明的新未来
- (05)System Verilog 组合逻辑与时序逻辑区别
- android系统打印功能实现,Android实现系统打印功能
- Silverlight 2 DataGrid December 2008
- 三菱plc pwm指令_西门子PLC常见的20个问题,你碰到过几个?
- LeetCode:35. Search Insert Position(Easy)
- vi/vim命令使用
- C语言编程题——杨辉三角
- Vue01介绍+数据双向绑定+生命周期+什么是BootCDN+代码示例
- java千万级别数据处理(2)-千万级别FTP下载
- 130行Python代码模仿“蚂蚁呀嘿”特效,太魔性了!
- 2021年育婴员(五级)复审考试及育婴员(五级)实操考试视频