如题 tomcat启动成功 在外部浏览器可以访问localhost:8080但不能访问项目,内置浏览器都不能访问
web.xml部署
<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*:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
使用的注解
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/list")
public ModelAndView itemsList()throws Exception{
List<Items> list = itemsService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("itemList");
return modelAndView;
}
}
web.xml部署
<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*:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
使用的注解
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/list")
public ModelAndView itemsList()throws Exception{
List<Items> list = itemsService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("itemList");
return modelAndView;
}
}
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
意思是.action结尾的请求才会被springmvc拦截。
改成:
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
例如 http://localhost:8080/Test/hello.action
2、你的配置中没有配置<welcome-file-list>,如果你要直接访问应该要加这个
例如 <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
3、正常访问是http://localhost:端口号/项目名称/ 或者 http://localhost:端口号/项目名称/请求名
http://localhost:端口号/项目名称/ 有<welcome-file-list>的情况 例如:http://localhost:8080/Test/
http://localhost:端口号/项目名称/请求名 例如http://localhost:8080/Test/hello