首页 > 代码库 > Eclipse use maven S3

Eclipse use maven S3

【LJ?Dragon】用户控制层

【相关链接】用户业务逻辑层: http://www.cnblogs.com/anstoner/p/6398637.html

1. 新建 webapp maven工程

1.1 编写 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>cn.anstone</groupId>	<artifactId>user-web</artifactId>	<packaging>war</packaging>	<version>0.0.1-SNAPSHOT</version>	<name>user-web Maven Webapp</name>	<url>http://maven.apache.org</url>	<dependencies>		<!-- 配置本地依赖 -->		<dependency>			<groupId>cn.anstone</groupId>			<artifactId>user-services</artifactId>			<version>0.0.1-SNAPSHOT</version>		</dependency>		<!-- 添加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>		<!-- 添加jtl支持 -->		<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>junit</groupId>			<artifactId>junit</artifactId>			<version>4.12</version>			<scope>test</scope>		</dependency>	</dependencies>	<build>		<finalName>user-web</finalName>	</build></project>

 

1.2 编写 用户控制层 方法

/user-web/src/main/java/cn/anstone/controller/UserController.java
package cn.anstone.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 cn.anstone.entity.User;import cn.anstone.services.UserService;/*** 描述:用户控制层方法** @author Anstone* * @Date Feb 14, 2017*/@Controller@RequestMapping("/user")public class UserController {		@Autowired	private UserService userService;		@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";		}	}}

1.3 编写 spring-mvc 配置文件

/user-web/src/main/resources/spring-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="cn.anstone.controller" />	<!-- 视图解析器 -->	<bean id="viewResolver"		class="org.springframework.web.servlet.view.InternalResourceViewResolver">		<property name="prefix" value="http://www.mamicode.com/" />		<property name="suffix" value="http://www.mamicode.com/.jsp"></property>	</bean></beans>  

 

 

1.4 编写 web.xml 配置文件

/user-web/src/main/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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>MyBatisPro05</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>

 

 

1.5 编写 Jsp 页面

/user-web/src/main/webapp/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="http://www.mamicode.com/${user.userName }"/><br/>	password:<input type="password" name="password" value="http://www.mamicode.com/${user.password }"><br/>	<input type="submit" value="http://www.mamicode.com/login"/><font color="red">${errorMsg }</font></form></body></html>

 

/user-web/src/main/webapp/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>

Eclipse use maven S3