1.使用maven创建web项目,配置web.xml。
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>spring3mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
2.加入spring dependency,同时别忘记加入servlet的dependency,否则@Controller不能被正常引用。
<!-- Dependencies --> <dependencies> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> <scope>runtime</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Other dependencies --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> </dependency> </dependencies>
3.配置spring的config文件。
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/> <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 自动搜索@Controller标注的类 --> <context:component-scan base-package="org.sagaris.actions"/> <!-- Default ViewResolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.加入Controller控制器类,并加入Annotaion映射。
@Controller @RequestMapping("/demo") public class DemoAction { @RequestMapping(value = "/{id}",method= RequestMethod.GET) public String helloWorld(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) { //System.out.println("asdf"+id); request.setAttribute("message", "You Input Topci Id is: <b>"+id+"</b>"); return "demo" ; //"demo"是转向页面的名称,位置跟spring config文件里配置的 “InternalResourceViewResolver”有关 }
controller之间跳转
@Controller
@RequestMapping("/user") public class UserController { @RequestMapping(value="/login") public String test(HttpServletRequest request, HttpServletResponse response,Users userinfo){ // 非常方便可以直接在方法里面放入对象 if (userinfo.getUsername().equals("username") && userinfo.getPassword().equals("password") ) { request.setAttribute("user", userinfo); return "users/list"; //判断,将跳转不同的页面 } else{ return "users/loginerr"; //判断,将跳转不同的页面 } } }
5.配置jetty plugin 。
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins>
6.使用maven命令运行jetty服务器,测试。