SpringMVC Annotation的问题

J2EE 码拜 9年前 (2015-04-17) 1555次浏览 0个评论

——————————————————————————————————————————————
**********************************************web.xml********************************************************
——————————————————————————————————————————————

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>*</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>*</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

——————————————————————————————————————————————
**********************************************hello-servlet.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:context=”http://www.springframework.org/schema/context”
    xmlns:mvc=”http://www.springframework.org/schema/mvc”
    xsi:schemaLocation=”
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>
        
        <context:annotation-config/>
        <context:component-scan base-package=”com.alvin.servlet”/>
        <mvc:annotation-driven></mvc:annotation-driven>
        
        <bean id=”/welcome.htm” class=”com.alvin.servlet.Welcome”></bean>
        
        <bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
            <property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/>
            <property name=”prefix” value=”/WEB-INF/jsp/”/>
            <property name=”suffix” value=”.jsp“/>
        </bean>
        
 </beans>

——————————————————————————————————————————————
**********************************************HelloController***********************************************
——————————————————————————————————————————————

package com.alvin.servlet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(“/hc”)
public class HelloController {

@RequestMapping(“/hello”)
public String hello(){
System.out.println(“hello”);
return “hello”;
}

}

——————————————————————————————————————————————
*************************************************Student*****************************************************
——————————————————————————————————————————————

package com.alvin.model;

import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.Size;

public class Student {

private int id;
private String name;
private int age;
private double score;

public Student(){
super();
}
public Student(int id, String name, int age, double score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}

@NotEmpty(message=”ID不得为空”)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NotEmpty(message=”Name不得为空”)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotEmpty(message=”年龄不得为空”)
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Size(max=100,min=0,message=”成绩只能在0~100之间”)
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}

}

——————————————————————————————————————————————
*************************************************Student*****************************************************
——————————————————————————————————————————————

package com.alvin.servlet;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alvin.model.Student;

@Controller
@RequestMapping(“/student”)
public class StudentController {

private Map<String,Student> studentModel=new HashMap<String,Student>();

public StudentController(){
studentModel.put(“tom”, new Student(1,”tom”,18,95.50));
studentModel.put(“alice”, new Student(2,”alice”,18,95.50));
studentModel.put(“johny”, new Student(3,”johny”,18,95.50));
studentModel.put(“dave”, new Student(4,”dave”,18,95.50));
studentModel.put(“pertty”, new Student(5,”pertty”,18,95.50));
}

@RequestMapping(value=”/list”,method=RequestMethod.GET)
public String list(Model model){
model.addAttribute(“students”, studentModel);
return “student/list”;
}

}

——————————————————————————————————————————————
*************************************************list*****************************************************
——————————————————————————————————————————————
SpringMVC Annotation的问题
SpringMVC Annotation的问题
SpringMVC Annotation的问题

tomcat启动无报错,为什么打开浏览器输入localhost:8080/*/hc/hello服务端就跳转到hello.jsp页面
但是输入localhost:8080/*/student/list 就报404错误呢?

SpringMVC Annotation的问题
40分
<property name=”prefix” value=”/WEB-INF/jsp/”/>
你配置了这个,那就应该在jsp目录下建student目录才对。
SpringMVC Annotation的问题
/WEB-INF/jsp/student/list.jsp最后的结果是这样的,当然错了,你把你的student目录移到jsp中

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SpringMVC Annotation的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!